package com.lemon.smartsafety.utils.common;

/**
 * Created by Cooliean on 15/8/7.
 */

import java.util.Date;

/**
 * 关于格式转换以及判断的类
 */
public final class Convertor {

    public Convertor() {
    }

    /**
     * 转换成bool值
     *
     * @param sVariant
     */
    public static boolean toBool(String sVariant) {
        return !(sVariant == null || sVariant.equalsIgnoreCase("0") || sVariant.equalsIgnoreCase("false") || sVariant.equalsIgnoreCase("否") || sVariant.length() == 0);
    }

    /**
     * 转换成bool值
     *
     * @param iVariant
     */
    public static boolean toBool(int iVariant) {
        return (iVariant != 1);
    }

    /**
     * 转换成int值，默认值是0
     *
     * @param str
     */
    public static int toInt(String str) {
        return Convertor.toInt(str, 0);
    }

    /**
     * 转换成int值
     *
     * @param str      字符串表达式
     * @param nDefault 假如转换失败，返回nDefault
     */
    public static int toInt(String str, int nDefault) {
        if (!isNumeric(str)) {
            return nDefault;
        } else if (str.indexOf(".") != -1) {
            str = str.substring(0, str.indexOf("."));
        }
        return Integer.parseInt(str);
    }

    /**
     * 转换成long值，默认值是0
     *
     * @param str 字符串表达式
     */
    public static long toLong(String str) {
        return Convertor.toLong(str, 0);
    }

    /**
     * 转换成long值
     *
     * @param str      字符串表达式
     * @param nDefault 假如转换失败，返回nDefault
     */
    public static long toLong(String str, long nDefault) {
        if (!isNumeric(str)) {
            return nDefault;
        } else if (str.indexOf(".") != -1) {
            str = str.substring(0, str.indexOf("."));
        }
        return Long.parseLong(str);
    }

    /**
     * 转换成double值，默认值是0
     *
     * @param str 字符串表达式
     */
    public static double toDouble(String str) {
        return Convertor.toDouble(str, 0);
    }

    /**
     * 转换成double值
     *
     * @param str      字符串表达式
     * @param nDefault 假如转换失败，返回nDefault
     */
    public static double toDouble(String str, double nDefault) {
        return Convertor.toDouble(str, nDefault, 9);
    }

    /**
     * 转换成double值
     *
     * @param str        字符串表达式
     * @param nDefault   假如转换失败，返回nDefault
     * @param nPrecision 精度，指返回小数点后面第几位
     */
    public static double toDouble(String str, double nDefault, int nPrecision) {
        if (!isNumeric(str)) {
            return nDefault;
        }
        double nRet = Double.parseDouble(str);
        nRet = Math.round(nRet * Math.pow(10, nPrecision)) / Math.pow(10.0, nPrecision);
        return nRet;
    }

    /**
     * 按照要求的精度，转换成double值
     *
     * @param n
     * @param nPrecision 精度，指返回小数点后面第几位
     * @return doucle值
     */
    public static double toDouble(double n, int nPrecision) {
        n = Math.round(n * Math.pow(10, nPrecision)) / Math.pow(10.0, nPrecision);
        return n;
    }

    /**
     * 判断是否为数字类型
     *
     * @param str
     */
    public static boolean isNumeric(String str) {
        if (str == null) {
            return false;
        }
        if (str.length() == 0) {
            return false;
        }
        try {
            Double.parseDouble(str);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * 判断是否为int类型
     *
     * @param str
     */
    public static boolean isInt(String str) {
        return str != null && str.indexOf('.') == -1 && isNumeric(str) && str.length() <= 10;
    }

    /**
     * 判断是否为int类型
     *
     * @param str
     */
    public static boolean isLong(String str) {
        return str != null && str.indexOf('.') == -1 && isNumeric(str);
    }

    /**
     * 转换成日期对象
     *
     * @param sDate
     */
    public static Date toDate(String sDate) {
        Date d = new Date();
        d.setYear(Integer.parseInt(DateUtils.getYear(sDate)));
        d.setMonth(Integer.parseInt(DateUtils.getMonth(sDate)) - 1);
        d.setDate(Integer.parseInt(DateUtils.getDate(sDate)));
        d.setHours(Integer.parseInt(DateUtils.getHour(sDate)));
        d.setMinutes(Integer.parseInt(DateUtils.getMinute(sDate)));
        d.setSeconds(Integer.parseInt(DateUtils.getSecond(sDate)));
        //d.setYear(Integer.parseInt(DateUtil.getYear(sDate));

        return d;
    }

    /*
     * 转换成大写的数字
     * @param nNum：待转转的数
     */
    public static String numToUpper(int nNum) {
        return Convertor.numToUpper("" + nNum);
    }

    /*
     * 转换成大写的数字
     * @param nNum：待转转的数
     */
    public static String numToUpper(String sNum) {
        String sUnit      = "千百十亿千百十万千百十";
        String sNumChar   = "零一二三四五六七八九";
        int    pos        = sNum.indexOf('.');
        String sBeforeDot = "";
        String sAfterDot  = "";
        if (pos == -1) {
            sBeforeDot = sNum;
        } else {
            sBeforeDot = sNum.substring(0, pos);
            sAfterDot = sNum.substring(pos + 1);
        }
        String sRet = "";
        char   c    = 0;
        for (int i = 0; i < sBeforeDot.length(); i++) {
            c = sBeforeDot.charAt(i);
            if (c == '0') {
                if (i < sBeforeDot.length() - 1) {
                    String sTmp = sUnit.substring(sUnit.length() - sBeforeDot.length() + i + 1, sUnit.length() - sBeforeDot.length() + i + 2);
                    if (sTmp.equals("亿") || sTmp.equals("万")) {
                        if (sRet.substring(sRet.length() - 1).equals("零")) {
                            sRet = sRet.substring(0, sRet.length() - 1);
                        }
                        if ((sRet.substring(sRet.length() - 1).equals("亿") || sRet.substring(sRet.length() - 1).equals("万")) && sTmp.equals("万")) {
                        } else {
                            sRet += sTmp;
                        }

                    } else if (!sRet.substring(sRet.length() - 1).equals("零")) {
                        sRet += "零";
                    }
                }

            } else {
                sRet += sNumChar.substring(c - 48, c - 48 + 1);
                if (i < sBeforeDot.length() - 1) {
                    sRet += sUnit.substring(sUnit.length() - sBeforeDot.length() + i + 1, sUnit.length() - sBeforeDot.length() + i + 2);
                }
            }
        }
        if (sRet.length() > 2 && sRet.substring(0, 2).equals("一十")) {
            sRet = sRet.substring(1);
        }
        if (sRet.substring(sRet.length() - 1).equals("零")) {
            sRet = sRet.substring(0, sRet.length() - 1);
        }
        if (sAfterDot.length() > 0) {
            sRet += "点";
            for (int i = 0; i < sAfterDot.length(); i++) {
                c = sAfterDot.charAt(i);
                sRet += sNumChar.substring(c - 48, c - 48 + 1);
            }
        }
        return sRet;

    }

    /**
     * 字符串转Double
     *
     * @param s
     * @return
     */
    public static double parserToDouble(String s) {
        double result = 0;
        try {
            result = Double.valueOf(s);
            return result;
        } catch (Exception e) {
        }
        return 0;
    }
}
